<?php

namespace app\admin\controller\user;

use app\admin\model\rush\Time;
use app\common\controller\Backend;
use fast\Tree;

/**
 * 粉丝管理
 *
 * @icon fa fa-user
 */
class Fans extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id,username,nickname';

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 查看
     */
    public function index()
    {
        $this->assignconfig('inviter_id',$this->request->get('inviter_id'));
        $this->assignconfig('time_list',Time::column('name','id'));
        return $this->view->fetch();
    }

    /**
     * 粉丝
     */
    public function fans()
    {
        $this->model = model('User');
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //查找粉丝和本身ID
            $inviter_id = $this->request->get('inviter_id');
            $fans_ids = $this->model->where('inviter_id',$inviter_id)->column('id');
            $fans_ids[] = $inviter_id;

            $list = $this->model
                ->where($where)
                ->where('inviter_id','in',array_unique($fans_ids))
                ->order($sort, $order)
                ->field("*,if(inviter_id={$inviter_id},'一级粉丝','二级粉丝') relation")
                ->paginate($limit);
            $result = array(
                "total" => $list->total(),
                "rows" => $list->items(),
                //无限下级粉丝数量
                "fans_total" => count(
                    Tree::instance()
                        ->init(db('user')->field('id,inviter_id')->select(),'inviter_id')
                        ->getChildren($inviter_id)
                )
            );
            return json($result);
        }
        return $this->view->fetch('index');
    }

    /**
     * 粉丝订单
     */
    public function orderList()
    {
        $this->model = model('\app\admin\model\rush\Order');
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //查找粉丝和本身ID
            $inviter_id = $this->request->get('inviter_id');
            $fans_ids = model('User')->where('inviter_id',$inviter_id)->column('id');
            $fans_ids[] = $inviter_id;

            $list = $this->model
                ->with(['buyer','seller'])
                ->where($where)
                ->where('buyer.inviter_id','in',array_unique($fans_ids))
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','order_no','order_status','payment_voucher','complain_status','express_company','express_no','createtime','rushtime','goods_name','goods_price','goods_image','time_name']);
                $row->visible(['buyer']);
                $row->getRelation('buyer')->visible(['bank_name','bank_card_no','bank_card_holder','nickname','mobile']);
                $row->visible(['seller']);
                $row->getRelation('seller')->visible(['bank_name','bank_card_no','bank_card_holder','nickname','mobile']);
            }

            $result = array(
                "total" => $list->total(),
                "rows" => $list->items(),
                "price_total" => $this->model
                    ->with(['buyer'])
                    ->where($where)
                    ->where('buyer.inviter_id','in',array_unique($fans_ids))
                    ->sum('goods_price')
            );
            return json($result);
        }
        return $this->view->fetch('index');
    }

    /**
     * 抢购进场数据统计
     */
    public function statistic()
    {
        $this->model = model('\app\admin\model\rush\Statistic');
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //查找粉丝和本身ID
            $inviter_id = $this->request->get('inviter_id');
            $fans_ids = model('User')->where('inviter_id',$inviter_id)->column('id');
            $fans_ids[] = $inviter_id;

            $table_name = $this->model->getTable();
            $list = $this->model
                ->with(['user'])
                ->where($where)
                ->where('user.inviter_id','in',array_unique($fans_ids))
                ->whereTime($table_name.'.updatetime','today')
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','time_id','updatetime']);
                $row->visible(['user']);
                $row->getRelation('user')->visible(['nickname','mobile','avatar']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch('index');
    }

    /**
     * 粉丝抢购成功统计
     */
    public function rushSuccess()
    {
        $this->model = model('\app\admin\model\rush\Order');
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            //查找粉丝和本身ID
            $inviter_id = $this->request->get('inviter_id');
            $fans_ids = model('User')->where('inviter_id',$inviter_id)->column('id');
            $fans_ids[] = $inviter_id;

            $table_name = $this->model->getTable();
            $list = $this->model
                ->with(['buyer'])
                ->where($where)
                ->where('buyer.inviter_id','in',array_unique($fans_ids))
                ->whereTime($table_name.'.rushtime','today')
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','order_no','goods_name','rushtime','buyer_id','time_id']);
                $row->visible(['buyer']);
                $row->getRelation('buyer')->visible(['nickname','mobile','avatar']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch('index');
    }
}
